/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.base.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.modules.base.dao.CustomerGroupDetailDao;
import com.chlitina.store.modules.base.entity.CustomerGroupDetail;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 客户组详情Service
 * @author lvbo
 * @version 2013-07-23
 */
@Component
@Transactional(readOnly = true)
public class CustomerGroupDetailService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(CustomerGroupDetailService.class);
	
	@Autowired
	private CustomerGroupDetailDao customerGroupDetailDao;
	
	public Page<CustomerGroupDetail> find(Page<CustomerGroupDetail> page, CustomerGroupDetail customerGroupDetail, String groupId) {
		DetachedCriteria dc = customerGroupDetailDao.createDetachedCriteria();
		if (groupId != null) {
			dc.add(Restrictions.eq("customerGroup.id", Long.parseLong(groupId)));
		}
		dc.createAlias("customer", "c");
		Store store = StoreUtils.getStore();
		dc.add(Restrictions.eq("c.store.id", store.getId()));
		dc.addOrder(Order.desc("customer.id"));
		return customerGroupDetailDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(CustomerGroupDetail customerGroupDetail) {
		customerGroupDetailDao.save(customerGroupDetail);
	}
	
	@Transactional(readOnly = false)
	public List<CustomerGroupDetail> getByGroupId(Long groupId) {
		DetachedCriteria dc = customerGroupDetailDao.createDetachedCriteria();
		if (groupId != null) {
			dc.add(Restrictions.eq("customerGroup.id", groupId));
		}
		dc.createAlias("customer", "c");
		Store store = StoreUtils.getStore();
		dc.add(Restrictions.eq("c.store.id", store.getId()));
		return customerGroupDetailDao.find(dc);
	}
	@Transactional(readOnly = false)
	public CustomerGroupDetail getByPkId(Long groupId,Long customerId) {
		DetachedCriteria dc = customerGroupDetailDao.createDetachedCriteria();
		if (groupId != null) {
			dc.add(Restrictions.eq("customerGroup.id", groupId));
		}
		if (customerId != null) {
			dc.add(Restrictions.eq("customer.id", customerId));
		}
		dc.createAlias("customer", "c");
		Store store = StoreUtils.getStore();
		dc.add(Restrictions.eq("c.store.id", store.getId()));
		List<CustomerGroupDetail> list = customerGroupDetailDao.find(dc);
		return list != null && list.size() > 0 ? list.get(0) : null;
	}
	
	@Transactional(readOnly = false)
	public void delete(CustomerGroupDetail customerGroupDetail) {
		customerGroupDetailDao.delete(customerGroupDetail);
	}
	
}
